package ${package}.${rootArtifactId}.controller.system;

import com.alibaba.fastjson.JSON;
import ${package}.${rootArtifactId}.common.model.Result;
import ${package}.${rootArtifactId}.common.util.RequestContextUtil;
import ${package}.${rootArtifactId}.model.param.LoginParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

/**
 * @author weidong.lin
 * @date 2021-07-07 15:16:28
 */
@Controller
@Slf4j
public class LoginController {

    @PostMapping(value = "/wapi/login")
    @ResponseBody
    public Result login(@RequestBody LoginParam param) {
        Map<String, String> userMap = new HashMap<>();
        userMap.put("account", "admin");
        userMap.put("name", "admin");
        userMap.put("password", "123456");
        return Result.success(userMap);
    }

    @PostMapping(value = "/wapi/ldap_user/grant_authorization")
    @ResponseBody
    public Result grantAuthorization(HttpServletRequest request) {
        try {
            return Result.success();
        } catch (Exception e) {
            return Result.failure("白名单授权失败");
        }
    }

    @PostMapping(value = "/wapi/login_ldap")
    @ResponseBody
    public Result loginByLdap(@RequestBody LoginParam param) {
        Map<String, String> userMap = new HashMap<>();
        userMap.put("account", "admin");
        userMap.put("name", "admin");
        userMap.put("password", "123456");
        return Result.success(userMap);
    }

    @RequestMapping("/wapi/modules")
    @ResponseBody
    public Result modules(HttpServletRequest request) {
        return Result.success();
    }

    @RequestMapping("/wapi/password")
    @ResponseBody
    public Result password(HttpServletRequest request) {
        request.getSession().invalidate();
        return Result.success();
    }

    @RequestMapping("/wapi/logout")
    @ResponseBody
    public Result loginOut(HttpServletRequest request) {
        request.getSession().invalidate();
        return Result.success();
    }
}
